﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
using Leo.Entities;
using Leo.BL;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Leo.Cliente.Controllers
{
    public class LaboratorioController : Controller
    {
        //
        // GET: /Laboratorio/
        [Dependency]
        public ILaboratorioBL laboratorioBL { get; set; }

        public LaboratorioController()
        { 
        }

        public ActionResult Index()
        {
            var laboratorios = laboratorioBL.GetFromLaboratorioByCriterio("");
            return View(laboratorios);
        }

        [HttpPost]
        public ActionResult Index(string criterio = "")
        {
            var laboratorios = laboratorioBL.GetFromLaboratorioByCriterio(criterio);

            return Request.IsAjaxRequest() ?
                (ActionResult)PartialView("_resultLaboratorios", laboratorios) : View(laboratorios);
        }
        public ActionResult Details(Int32 id)
        {
            var laboratorio = laboratorioBL.GetFromLaboratoriosById(id);

            return PartialView("_details",laboratorio);
        }

        [HttpPost]
        public ActionResult Add(Laboratorio laboratorio)
        {
            var ok = false;
            var msg = "ERROR";

            if (ModelState.IsValid)
            {
                laboratorioBL.AddLaboratorio(laboratorio);
                ok = true;
                msg = "Laboratorios Guardado Corectamente";
            }
            var rpt = new
            {
                ok = ok,
                msg = msg
            };

            return Json(rpt,JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(Int32 id)
        {
            laboratorioBL.RemoveLaboratorio(id);

            var laboratorios = laboratorioBL.GetFromLaboratorioByCriterio("");
            return PartialView("_resultLaboratorios",laboratorios);
        }

        [HttpPost]
        public ActionResult Edit(Laboratorio laboratorio)
        {
            var ok = false;
            var msg = "ERROR";
            
            if (ModelState.IsValid)
            {
                laboratorioBL.UpdateLaboratorio(laboratorio);
                ok = true;
                msg = "Laboratorio Actualizado";
            }
            var rpt = new
            {
                ok = ok,
                msg = msg
            };

            return Json(rpt,JsonRequestBehavior.AllowGet);
        }
    }
}
